package cz.data.common.database.metadata.parser;

import cz.data.common.exception.DataException;
import cz.data.common.support.metadata.MysqlDataTypeEnum;
import cz.data.common.support.ColumnParser;
import cz.data.common.support.DataType;
import cz.data.common.support.DataTypeEnum;

import java.util.Optional;

public class MysqlColumnParser implements ColumnParser {

    @Override
    public DataType parse(DataTypeEnum dataTypeEnum) {
        MysqlDataTypeEnum typeEnum = MysqlDataTypeEnum.match(dataTypeEnum.getValue(), MysqlDataTypeEnum.VARCHAR);
        switch (typeEnum) {
            case TINYINT:
                return new MysqlTinyintDataType();
            case INT:
                return new MysqlIntDataType();
            case BIGINT:
                return new MysqlBigintDataType();
            case FLOAT:
                return new MysqlFloatDataType();
            case DOUBLE:
                return new MysqlDoubleDataType();
            case DECIMAL:
                return new MysqlDecimalDataType();
            case CHAR:
                return new MysqlCharDataType();
            case VARCHAR:
                return new MysqlVarcharDataType();
            case TEXT:
                return new MysqlTextDataType();
            case DATE:
                return new MysqlDateDataType();
            case TIME:
                return new MysqlTimeDataType();
            case YEAR:
                return new MysqlYearDataType();
            case DATETIME:
                return new MysqlDatetimeDataType();
            case BLOB:
                return new MysqlBlobDataType();
            case TIMESTAMP:
                return new MysqlTimestampDataType();
            default:
                throw new DataException("字段数据类型错误");
        }
    }

    public static class MysqlBigintDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "bigint";
        }
    }

    public static class MysqlBlobDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "blob";
        }
    }

    public static class MysqlCharDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "char(" + Optional.ofNullable(columnLength).orElse("1") + ")";
        }
    }

    public static class MysqlDateDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "date";
        }
    }

    public static class MysqlDatetimeDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "datetime";
        }
    }

    public static class MysqlDecimalDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "decimal(" + Optional.ofNullable(columnLength).orElse("10") + ", " + Optional.ofNullable(columnScale).orElse("2") + ")";
        }
    }

    public static class MysqlDoubleDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "double(" + Optional.ofNullable(columnLength).orElse("5") + ", " + Optional.ofNullable(columnScale).orElse("2") + ")";
        }
    }

    public static class MysqlFloatDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "float(" + Optional.ofNullable(columnLength).orElse("5") + ", " + Optional.ofNullable(columnScale).orElse("2") + ")";
        }
    }

    public static class MysqlIntDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "int";
        }
    }

    public static class MysqlTextDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "text";
        }
    }

    public static class MysqlTimeDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "time";
        }
    }

    public static class MysqlTimestampDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "TIMESTAMP";
        }
    }

    public static class MysqlTinyintDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "tinyint";
        }
    }

    public static class MysqlVarcharDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "varchar(" + Optional.ofNullable(columnLength).orElse("255") + ")";
        }
    }

    public static class MysqlYearDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "year";
        }
    }
}
